package kino.command;

import org.springframework.util.StringUtils;
import kino.client.KinoClient;
import kino.common.KinoConstant;
import kino.database.KinoDB;
import kino.object.KinoObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@KinoCommandEnable
public class HgetAllCommand extends AbstractKinoCommand{
    @Override
    public String doExecute(KinoClient client, String... argv) {
        KinoDB kinoDB = client.getDb();
        Map<String, KinoObject> dict = kinoDB.getDict();
        if (dict.containsKey(argv[0])) {
            Object key = dict.get(argv[0]).getObj();
            List<Object> resultList = new ArrayList<>();
            if (key instanceof Map) {
                Map<String, String> hash = (HashMap<String, String>)key;
                StringBuffer sb = new StringBuffer();
                for (String filed : hash.keySet()) {
                    resultList.add(filed);
                    resultList.add(hash.get(filed));
                }
                return commandEncoder.encode(resultList);
            }
            return KinoConstant.KEY_TYPE_ERR;
        }
        return KinoConstant.NIL;
    }

    @Override
    public Integer getArgc() {
        return 1;
    }

    @Override
    public String getCommandName() {
        return "hgetall";
    }

    @Override
    public boolean expireCheck() {
        return true;
    }

    @Override
    public boolean aofFlag() {
        return false;
    }

    @Override
    public boolean hasExtParam() {
        return false;
    }
}
